// API配置文件
export const API_ENDPOINTS = {
  // 帖子相关
  POSTS: {
    LIST: '/posts/posts/',
    DETAIL: (id) => `/posts/posts/${id}/`,
    LIKE: (id) => `/posts/${id}/like/`,
    STAR: (id) => `/posts/${id}/star/`,
  },
  
  // 评论相关
  COMMENTS: {
    LIST: '/posts/comments/',
    CREATE: '/posts/comments/',
    DELETE: (id) => `/posts/comments/${id}/`,
    BY_POST: (postId) => `/posts/comments/?post_id=${postId}`,
  },
  
  // 用户操作相关
  USER_ACTIONS: {
    GET: '/user/actions/',
    SYNC: '/user/actions/sync/',
  },
  
  // 用户相关
  USER: {
    PROFILE: '/user/profile/',
    LOGIN: '/user/login/',
    LOGOUT: '/user/logout/',
  }
}

// 请求头配置
export const getAuthHeaders = () => {
  const token = localStorage.getItem('auth_token')
  return token ? { 'Authorization': `Bearer ${token}` } : {}
}

// 响应拦截器
export const handleResponse = (response) => {
  if (response.status >= 200 && response.status < 300) {
    return response.data
  } else {
    throw new Error(`HTTP ${response.status}: ${response.statusText}`)
  }
}

// 错误处理
export const handleError = (error) => {
  console.error('API Error:', error)
  if (error.response) {
    // 服务器返回错误状态码
    const { status, data } = error.response
    switch (status) {
      case 401:
        throw new Error('未授权，请重新登录')
      case 403:
        throw new Error('权限不足')
      case 404:
        throw new Error('资源不存在')
      case 500:
        throw new Error('服务器内部错误')
      default:
        throw new Error(data?.message || `请求失败 (${status})`)
    }
  } else if (error.request) {
    // 网络错误
    throw new Error('网络连接失败，请检查网络设置')
  } else {
    // 其他错误
    throw new Error(error.message || '未知错误')
  }
} 